//app.js
App({
  globalData: {
    //app名称
    app:'',
    lock:true, 
    appid:'',
    // http://www.hg.com/api（测试站）
    // https://x.hgshow.cn/api（正式站）
    api:'https://x.hgshow.cn/api',
    srcApi:'https://x.hgshow.cn',
    //经纬度
    lat:0,
    lng:0,
    //客户联系方式
    mobile:'',
    //session_key
    key:'',
    //当前路由位置
    route:'',
    //微信授权信息
    info:false
  },
  onLaunch: function () {
    let _this=this
    
    this.login()

    //监听网络剧状态
    wx.onNetworkStatusChange(function (res){
      if(res.isConnected){
        _this.login()
      }
    })
    wx.getExtConfig({
      success: function (res) {
        console.log(res)
        if (res.extConfig.extAppid){
          _this.globalData.appid = res.extConfig.extAppid
        }
      }
    })
  },
  //小程序初始化
  login:function(){
    let _this=this
    //微信登录
    wx.login({
      success: function (res) {

        let code = res.code
        if (res.code) {
          //应用初始化获取用户信息
          wx.getUserInfo({
            success: function () {
              _this.globalData.info = true
            },
            complete: function (res) {
              // 获取session_key/提交用户信息
              let userInfo
              if (res.errMsg === 'getUserInfo:ok') {
                userInfo = res
              } else {
                userInfo = null
              }
              wx.request({
                url: _this.globalData.api + '/session_key?appid=' + _this.globalData.appid + '&code=' + code,
                method: 'POST',
                data: {
                  data: userInfo
                },
                success: function (res) {
                  let data = res.data.data
                  //判断session_key是否存在
                  if (data.session_key) {
                    _this.globalData.key = res.data.data.session_key
                    //app初始化
                    wx.request({
                      url: _this.globalData.api + '/config?appid=' + _this.globalData.appid,
                      success: function (res) {
                        let data = res.data.data
                        _this.globalData.app = data.appName
                        _this.globalData.lat = data.lat
                        _this.globalData.lng = data.lng
                        _this.globalData.mobile = data.mobile
                        wx.redirectTo({
                          url: '../index/index'
                        })
                      }
                    })
                  } else {
                    wx.redirectTo({
                      url: '/pages/error/error'
                    })
                  }
                }
              })
            },
          })
        }
      },
      //wx.login错误处理
      fail: function (res) {
        console.log(res)
        wx.redirectTo({
          url: '/pages/error/error'
        })
      }
    })
  },
  //导航菜单点击事件
  navClicked: function (e) {
    this.setData({
      nowNav: e.currentTarget.dataset.index
    })
  },
  //路由跳转
  routerGo: function (e) {
    let _this = this
    //同步锁处理
    if (this.globalData.lock) {
      this.globalData.lock=false
      wx.navigateTo({
        url: e.currentTarget.dataset.url+'?id='+e.currentTarget.dataset.id+'&from='+e.currentTarget.dataset.from,
        complete: function () {
          setTimeout(function () {
            _this.globalData.lock = true
          }, 1000)
        }
      })
    }
  }
  
})